/*
 * Settings.java
 *
 * Created on 30 de Agosto de 2007, 21:11
 *
 */

package rad.Java.Common.DB;
/**
 *
 * @author Evolution Systems - Do the Evolution
 */
import java.io.*;

public class Settings implements Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String Name;
	private String Host;
	private String User;
	private String Password;//TODO: criptografar a senha antes de armazenar  no disco
	private String Protocol;
	
	public void setDatabaseName(String dbName)
	{
		this.Name = dbName;
	}
	
	public void setDatabaseHost(String dbHost)
	{
		this.Host = dbHost;
	}
	
	public void setDatabaseUser(String dbUser)
	{
		this.User = dbUser;
	}

	public void setDatabasePassword(String dbPassword)
	{
		this.Password = dbPassword;
	}

	public void setDatabaseProtocol(String dbProtocol)
	{
		this.Protocol = dbProtocol;
	}

	public String getDatabaseName()
	{
		return Name;
	}
	
	public String getDatabaseHost()
	{
		return Host;
	}
	
	public String getDatabaseUser()
	{
		return User; 
	}

	public String getDatabasePassword()
	{
		return Password;
	}

	public String getDatabaseProtocol()
	{
		return Protocol;
	}

	public void writeSettings(String nameFile){
		try{
		   FileOutputStream out = new FileOutputStream(nameFile);
		   ObjectOutputStream ooOutput= null;	
			try{
				ooOutput= new ObjectOutputStream(out);
				ooOutput.writeObject(this);
			}
			catch(IOException e){
				System.out.println("IO Exception writeSettings-editSetings: \n"+e);
			}
		}
		catch(FileNotFoundException e){
			System.out.println("The file can't be created!!!");
		}
	}
	
	public boolean readSettings(String nameFile){
		Settings s = new Settings();
		ObjectInputStream oiInput= null;
		try{
			try{
			   FileInputStream in= new FileInputStream(nameFile);
				oiInput= new ObjectInputStream( in );
			}
			catch(FileNotFoundException e){
				System.out.println("File not found!");
                                return false;
			}	
			s= (Settings) oiInput.readObject();
			
			Name= s.getDatabaseName();
			Host= s.getDatabaseHost();
			User= s.getDatabaseUser();
			Password= s.getDatabasePassword();
			Protocol= s.getDatabaseProtocol();
		}  
		catch(Exception e){
			System.out.println("Exception readSettings-editSettings: \n"+e);
                        return false;
		}
                return true;
	}
	
        public static boolean settingsExists(String nameFile)
        {
            File file = new File(nameFile);
            if( file.exists())
                return true;
            return false;
        }
        
        public boolean clearSettings(String nameFile)
        {
            File file = new File(nameFile);
            if( file.exists())
                if(file.delete())
                    return true;
            return false;
        }
	public void showSettings()
	{
            System.out.println("***Settings***");
            System.out.println(Name);
            System.out.println(Host);
	   System.out.println(User);
	   System.out.println(Protocol);
	   System.out.println("***End settings***");
	}
}
   
